package com.tfjybj.itoo.exam.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.QuestionClassifyEntity;
import com.tfjybj.itoo.exam.model.QuestionClassifyModel;
import com.tfjybj.itoo.exam.provider.dao.QuestionClassifyDao;
import com.tfjybj.itoo.exam.provider.service.QuestionClassifyService;
import com.tfjybj.itoo.exam.provider.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * QuestionClassifyService接口实现类
 * ${base}表
 *
 * @author 李青松
 * @version 2.0.0
 * @since 2.0.0 2018-11-01 08:55:53
 */
@Service("questionClassifyService")
public class QuestionClassifyServiceImpl extends BaseServicePlusImpl<QuestionClassifyDao, QuestionClassifyEntity> implements QuestionClassifyService {

    //region 模板生成
    @Resource
    private QuestionClassifyDao questionClassifyDao;
    @Resource
    private QuestionService questionService;

    @Resource
    private QuestionClassifyService questionClassifyService;

    @Override
    public PageInfo<QuestionClassifyEntity> queryByLikePId(String pId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(questionClassifyDao.queryLikePId(pId));
    }


    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    @Override
    public List<QuestionClassifyEntity> queryClassify(String operator) {
        return questionClassifyDao.queryClassify(operator);
    }

    @Override
    public List<QuestionClassifyEntity> queryQuestionClassify(String operator, String courseId) {
        List<String> questionTypeId = questionService.queryQuestionClassifyType(operator, courseId);
        List<QuestionClassifyEntity> questionClassifyEntityList = new ArrayList<>();
        for (String id : questionTypeId) {
            QuestionClassifyEntity questionClassifyEntity = questionClassifyDao.selectById(id);
            if (questionClassifyEntity != null) {
                questionClassifyEntityList.add(questionClassifyEntity);
            }
        }
        return questionClassifyEntityList;
    }

    @Override
    public List<QuestionClassifyModel> queryQuestionClassfiyByQuestiontypeid(String courseid, String questiontypeid) {
        //子节点
        List<QuestionClassifyModel> questionClassifyModelList = questionClassifyDao.queryQuestionClassfiyByQuestiontypeid(courseid, questiontypeid);
        List<QuestionClassifyModel> questionClassifyModelList1 = questionClassifyDao.queryQuestionClassfiyByQuestiontypeid(courseid, questiontypeid);
        // 所有节点
        List<QuestionClassifyModel> questionClassifyModels = questionClassifyDao.queryQuestoinClassfiyByCourseid(courseid);

        //父类节点map
        Map<String, QuestionClassifyModel> questionClassifyModelMap = new HashMap<>();
        //所有节点的map
        for (QuestionClassifyModel questionClassifyModel : questionClassifyModels) {
            questionClassifyModelMap.put(questionClassifyModel.getId(), questionClassifyModel);
        }
        for (QuestionClassifyModel questionClassifyModel : questionClassifyModelList) {
            questionClassifyModelMap.remove(questionClassifyModel.getId());
        }

        for (int i = 0; i < questionClassifyModelList.size() + 1; i++) {
            List<QuestionClassifyModel> questionClassifyModelList2 = new ArrayList<>();
            for (QuestionClassifyModel questionClassifyModel : questionClassifyModelList) {
                QuestionClassifyModel questionClassifyModel1 = questionClassifyModelMap.get(questionClassifyModel.getParentId());
                questionClassifyModelList2.add(questionClassifyModel1);
                HashSet<QuestionClassifyModel> questionSetMap = new HashSet<>(questionClassifyModelList2);
                questionClassifyModelList2.clear();
                questionClassifyModelList2.addAll(questionSetMap);
            }
            questionClassifyModelList = questionClassifyModelList2;
            questionClassifyModelList1.addAll(questionClassifyModelList);
        }
        return questionClassifyModelList1;
    }

    @Override
    public List<QuestionClassifyEntity> queryByCourseId(String courseId) {
        return questionClassifyDao.queryByCourseId(courseId);
    }

    @Override
    public QuestionClassifyEntity queryByCourseIdAndLevel(String courseId, String operator){
        return questionClassifyDao.queryByCourseIdAndLevel(courseId,operator);
    }

    @Override
    public QuestionClassifyEntity getByName(String questionClassify, String courseId, String operator) {
        return questionClassifyDao.getByName(questionClassify, courseId,operator);
    }

    @Override
    public List<QuestionClassifyEntity> getByChapter(String chapter, String courseId) {
        return questionClassifyDao.getByChapter(chapter, courseId);
    }

    @Override
    public boolean createTreeModel(List<QuestionClassifyModel> questionClassifyModelList) {
        List<QuestionClassifyModel> models = resolveQuestionClassify(questionClassifyModelList);
        List<QuestionClassifyEntity> questionClassifyEntityList = new ArrayList<>();

        for (QuestionClassifyModel model : models) {
            QuestionClassifyEntity questionClassifyEntity = new QuestionClassifyEntity();
            BeanUtils.copyProperties(model, questionClassifyEntity);

            if (StringUtils.isEmpty(questionClassifyEntity.getParentId())) {
                questionClassifyEntity.setParentId("0");
            }
            if (StringUtils.isEmpty(questionClassifyEntity.getId())) {
                questionClassifyEntity.setId(IdWorker.getIdStr());
            }
            questionClassifyEntityList.add(questionClassifyEntity);
        }

        return questionClassifyService.saveBatch(questionClassifyEntityList);
    }


    // 分解试题分类集合Model
    private List<QuestionClassifyModel> resolveQuestionClassify(List<QuestionClassifyModel> questionClassifyModelList) {
        List<QuestionClassifyModel> classifyModelList = new ArrayList<>();
        for (QuestionClassifyModel model : questionClassifyModelList) {
            model.setId(IdWorker.getIdStr());
            model.getQuestionClassifyModel().remove(null);
            if (model.getQuestionClassifyModel() != null) {
                for (QuestionClassifyModel sonModel : model.getQuestionClassifyModel()) {
                    sonModel.setParentId(model.getId());
                }
                classifyModelList.addAll(resolveQuestionClassify(model.getQuestionClassifyModel()));
            }
            classifyModelList.add(model);
        }
        return classifyModelList;
    }


    @Override
    public List<QuestionClassifyEntity> queryTreeModel(String courseId, String operator) {
        return questionClassifyDao.queryTreeModel(courseId,operator);
    }


    /**
     * 根据课程id查询分类
     *
     * @param courseId
     * @return tooResult 根据课程id查询分类
     * @author 贾建清
     * @since 2.0.0 2018-12-17 14:55:49
     */
    @Override
    public List<QuestionClassifyModel> queryQuestionClass(String courseId) {
        return questionClassifyDao.queryQuestionClass(courseId);
    }

    /**
     * 根据操作人/课程/题型查询分类
     *
     * @param courseId
     * @param questionTypeId
     * @return ItooResult 根据操作人/课程/题型查询分类
     * @author 贾建清
     * @since 2.0.0 2018-12-17 19:48:48
     */
    @Override
    public List<QuestionClassifyModel> queryQuestionClassByType(String courseId, String questionTypeId) {
        return questionClassifyDao.queryQuestionClassByType(courseId, questionTypeId);
    }

    @Override
    public QuestionClassifyEntity createTreeNode(QuestionClassifyModel questionClassifyModel) {

        QuestionClassifyEntity questionClassifyEntity = new QuestionClassifyEntity();
        BeanUtils.copyProperties(questionClassifyModel, questionClassifyEntity);

        if (StringUtils.isEmpty(questionClassifyEntity.getParentId())) {
            questionClassifyEntity.setParentId("0");
        }
        if (StringUtils.isEmpty(questionClassifyEntity.getId())) {
            questionClassifyEntity.setId(IdWorker.getIdStr());
        }
        //查询level
        QuestionClassifyEntity classifyEntity = this.getById(questionClassifyModel.getParentId());
        if(classifyEntity==null || "0".equals(classifyEntity.getParentId())){
            questionClassifyEntity.setLevel(0);
        }else{
            questionClassifyEntity.setLevel(classifyEntity.getLevel()+1);
        }
        // 插入节点
        questionClassifyDao.insert(questionClassifyEntity);
        return questionClassifyEntity;
    }


}
